package operation;

import Dao.MilkDao;
import interfac.MilkTeaOperation;
import pojo.MilkTeaProduct;

import java.util.List;
import java.util.Scanner;

public class DeleteMilkTeaOperation implements MilkTeaOperation {

    @Override
    public void execute(Scanner scanner) {
        show_milk_tea();
        System.out.println("请输入要下架的奶茶产品ID:");
        int productIdToDelete = scanner.nextInt();

        MilkDao milk_dao = new MilkDao();
        MilkTeaProduct existingProduct = milk_dao.getProductById(productIdToDelete);

        if (existingProduct != null) {
            // 确认是否真的要删除此产品
            System.out.println("确认要下架 " + existingProduct.getProduct_name() + " 吗？(y/n)");
            String confirmation = scanner.next();
            if ("y".equalsIgnoreCase(confirmation)) {
                milk_dao.deleteProduct(productIdToDelete);
                System.out.println("奶茶已成功下架");
            } else {
                System.out.println("取消下架操作");
            }
        } else {
            System.out.println("输入的产品ID不存在，请检查后重新输入。");
        }
    }

    private static void show_milk_tea() {
        MilkDao milk_dao = new MilkDao();
        List<MilkTeaProduct> allProducts = milk_dao.getProducts();
        System.out.println("以下是店里的奶茶产品：");
        for (MilkTeaProduct product : allProducts) {
            System.out.println(product.getProduct_id() + " - " + product.getProduct_name() + "，价格：" + product.getPrice() + "，库存：" + product.getStock());
        }
    }
}
